/**
 * 我的书籍
 * @project readnote
 * @Title BookAction.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.center;

import java.util.List;

import com.cr.backbone.action.AppBaseActionSupport;
import com.opensymphony.xwork2.ActionContext;
import com.readnote.book.model.Book;
import com.readnote.book.model.UserBook;
import com.readnote.book.service.BookService;
import com.readnote.book.service.UserBookService;
import com.readnote.user.model.User;

public class UserBookAction extends AppBaseActionSupport{
	
	private static final String INDEX = "index";
	private static final String INPUT = "input";
	private static final String SUCCESS = "success";
	
	private User user;
	private Book book;
	private UserBook userBook;
	private List<UserBook> userBooks;
	private BookService bookService;
	private UserBookService userBookService;
	private boolean showAll=false;
	
	public boolean isShowAll() {
		return showAll;
	}

	public void setShowAll(boolean showAll) {
		this.showAll = showAll;
	}

	public String index(){
		if(!isShowAll()){
			//1、 当前登录用户笔记
			User user=getLoginInfo().getUser();
			Long userId=user ==null || user.isNullRow()?null:user.getId();
			getQuery().addQuery("eq_user.id_l", userId);
		}
		
		//2、倒序
		getQuery().addOrderDesc("id");
		page=this.userBookService.getPage(getQuery(), page);
		
//		Long id=getLoginInfo().getUser().getId();
//		user=new User(id);
//		userBooks = userBookService.getListUserBookByUser(user);
		return INDEX;
	}
	
	public String input(){
		if(!getLoginInfo().isLogin())
			return GLOBAL_LOGIN;
		
		Long id=getLoginInfo().getUser().getId();
		user=new User(id);
		if(book!=null && !book.isNullRow())
			book = bookService.get(book.getId());
		return INPUT;
	}
	
	public String save(){
		if(userBook.isNullRow()){
			userBookService.saveAddUserBook(book, user);
		}
		return SUCCESS;
	}
//	
//	public String delete(){
//		userBookService.delete(userBook);
//		return SUCCESS;
//	}
//	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public UserBook getUserBook() {
		return userBook;
	}

	public void setUserBook(UserBook userBook) {
		this.userBook = userBook;
	}

	public List<UserBook> getUserBooks() {
		return userBooks;
	}

	public void setUserBooks(List<UserBook> userBooks) {
		this.userBooks = userBooks;
	}

	public BookService getBookService() {
		return bookService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public UserBookService getUserBookService() {
		return userBookService;
	}

	public void setUserBookService(UserBookService userBookService) {
		this.userBookService = userBookService;
	}
	
}
